package com.zy.activity.adapter

import com.zy.utils.ActivityUtils.showViewTree
import androidx.recyclerview.widget.RecyclerView
import android.view.ViewGroup
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.zy.activity.R
import android.widget.TextView

enum class ShowType(val type: Int) {
    HEAD(0), COMMON(1);
}


data class FragmentListBean(
    val showType: ShowType = ShowType.COMMON,
    val title: String = "",
    val des: String = "",
    val code: Int = 0
)

class BaseFragmentAdapter(var mainBeanList: List<FragmentListBean>) :
    RecyclerView.Adapter<BaseFragmentAdapter.BaseViewHolder>() {

    private var clickCallBack: ClickCallBack? = null
    fun setClickCallBack(clickCallBack: ClickCallBack?) {
        this.clickCallBack = clickCallBack
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, type: Int): BaseViewHolder {
        return if (type == ShowType.HEAD.type) {
            val view = LayoutInflater.from(viewGroup.context)
                .inflate(R.layout.base_fragment_head_item, viewGroup, false)
            BaseHeadViewHolder(view)
        } else {
            val view = LayoutInflater.from(viewGroup.context)
                .inflate(R.layout.base_fragment_body_item, viewGroup, false)
            BaseBodyViewHolder(view)
        }
    }

    override fun onBindViewHolder(viewHolder: BaseViewHolder, position: Int) {
        viewHolder.bind(mainBeanList[position])
    }

    override fun getItemViewType(position: Int): Int {
        return mainBeanList[position].showType.type
    }

    override fun getItemCount(): Int {
        return mainBeanList.size
    }

    abstract inner class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        abstract fun bind(bean: FragmentListBean?)
    }

    internal inner class BaseHeadViewHolder(itemView: View) : BaseViewHolder(itemView) {
        private val title: TextView

        init {
            title = itemView.findViewById(R.id.base_fragment_head_item_title)
        }

        override fun bind(bean: FragmentListBean?) {
            if (bean != null) {
                title.text = bean.title
            }
        }
    }

    internal inner class BaseBodyViewHolder(itemView: View) : BaseViewHolder(itemView) {
        private val titleTv: TextView
        private val desTv: TextView
        private val iconIV: ImageView
        private val container: LinearLayout
        private var bean: FragmentListBean? = null

        init {
            titleTv = itemView.findViewById(R.id.item_title)
            desTv = itemView.findViewById(R.id.item_des)
            iconIV = itemView.findViewById(R.id.item_icon)
            container = itemView.findViewById(R.id.item_container)
            container.setOnClickListener { v ->
                showViewTree(v)
                clickCallBack?.let {
                    bean?.let { b ->
                        it.click(b)
                    }
                }
            }
        }

        override fun bind(bean: FragmentListBean?) {
            this.bean = bean
            bean?.let {
                titleTv.text = it.title
                desTv.text = it.des
                //iconIV.setBackgroundResource()
            }

        }
    }

    interface ClickCallBack {
        fun click(bean: FragmentListBean)
    }
}